package de.hauck.rtmidlet.views;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.TextField;

import de.hauck.rtmidlet.RTMidlet;
import de.hauck.rtmidlet.model.Configuration;
import de.hauck.rtmidlet.utils.Utils;

public class FormAdHoc extends Form implements CommandListener,
		ItemCommandListener {
	// private static Configuration c;

	// private RTMidlet _main;
	// private CanvasCharts _ftCanvas;
	private CanvasImage imageCanvas;
	private String chartName;
	private String[] showStockListArray; // = { "Realtime", "Time&Sales",
	// "Intraday", "FT" };
	private ChoiceGroup cg_charttype;
	private Command cShow = new Command("Show", Command.OK, 1);
	private Command cBack = new Command("Back", Command.BACK, 1);
	private TextField stockName;

	public FormAdHoc(RTMidlet rtmidlet) {
		super("AdHoc Form");
		// logger = MyLogger.getLogger();
		// _main = rtmidlet;
		showStockListArray = Configuration.getAvailableChartTypes();
		stockName = new TextField("Stock", "", 15, TextField.ANY);
		append(stockName);
		cg_charttype = new ChoiceGroup("Charttype",
					Choice.EXCLUSIVE, showStockListArray, null);
		cg_charttype.setItemCommandListener(this);
		append(cg_charttype);
		addCommand(cShow);
		addCommand(cBack);
		setCommandListener(this);
		
	}

	public void commandAction(Command command, Displayable dble) {
		if (command == cShow) {
			chartName = stockName.getString();
			if (chartName != null && !chartName.equals("")) {
				int selection = cg_charttype.getSelectedIndex();
				System.out.println("selected: " + selection);
				String url = Configuration.getURL(selection, chartName);
				url = Utils.replace(url, "<?time>", "0");
				System.out.println("Show " + url);
				// String[] urlArray = new
				// String[Configuration.getAvailableChartTypes().length];
				// for (int i=0;
				// i<Configuration.getAvailableChartTypes().length; i++) {
				// urlArray[i]=Configuration.getURL(i, chartName);
				// }
				String type = showStockListArray[cg_charttype
						.getSelectedIndex()];
				String[][] urls = { { url }, { type } };
				if (null == imageCanvas)
					imageCanvas = new CanvasImage(this, urls);
				else
					imageCanvas.setUrls(urls);
				imageCanvas.downloadPic(chartName, 0);
				RTMidlet.getInstance().show(imageCanvas);
			} else {
				RTMidlet.getInstance().setCurrent(this);
				RTMidlet.getInstance().alert("Info",
						"Please enter a stock symbol.",
						Configuration.ALERT_WAIT_TIME);
			}

		} else if (command == cBack) {
			RTMidlet.getInstance().showMenu();
		}
	}

	public void commandAction(Command c, Item item) {
		// not used
	}

}
